package org.example.netty.demo.chapter1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author ： minzhiwei
 * create by 2020/1/8 20:23
 * @description
 */
public class EchoServer {

    public static void main(String[] args) throws InterruptedException {
        final EchoHandler echoHandler = new EchoHandler();
        final NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
        final ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(eventExecutors, eventExecutors)
        .channel(NioServerSocketChannel.class)
        .localAddress(new InetSocketAddress(9999))
        .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
//                ch.pipeline().addLast(echoHandler);
                ch.pipeline().addLast(new SimpleEchoHandler());
            }
        });

        final ChannelFuture sync = serverBootstrap.bind().sync();
        sync.channel().closeFuture().sync();
        eventExecutors.shutdownGracefully().sync();
    }

}
